$(document).ready(function(){

    //Login Page Actions
    $('#loginPage').click(function(){
        if($('input#emailPage').val() == "" || $('input#passwordPage').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please enter an email and password!').fadeIn('slow', function(){
                setTimeout('$(\'#feedback\').fadeOut(\'slow\');', 5000);
            });
            return false;
        }
        else
        {
            $('#loading').fadeIn();
            $('#feedback').hide();

            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    email: $('input#emailPage').val(),
                    password: $('input#passwordPage').val(),
                    action: 'login'
                },
                success:function(data)
                {
                    if(data.error === true)
                    {
                        $('#loading').hide(400, function(){
                            $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn('slow', function(){
                                setTimeout('$(\'#feedback\').fadeOut(\'slow\');', 5000);
                            });
                        });
                    }
                    else
                    {
                        document.location="/sandbox1/myAccount.php";
                    }
                },
		error:function(XMLHttpRequest,textStatus,errorThrown)
		{
                    $('#loading').fadeOut('slow', function(){
                        $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again (login)!').fadeIn('slow', function(){
                            setTimeout('$(\'#feedback\').fadeOut(\'slow\');', 5000);
                        });
                    });

                }
            });
            return false;
        }
    });

    // Login Form Actions on Header
    $('#login').click(function(){
        if($('input#email').val() == "" || $('input#password').val() == "")
        {
            $('#loginFeedback').removeClass().addClass('error').text('Please enter an email and password!').fadeIn('slow', function(){
                setTimeout('$(\'#loginFeedback\').fadeOut(\'slow\');', 5000);
            });
            return false;
        }
        else
        {
            $('#loginLoading').fadeIn();
            $('#loginFeedback').hide();

            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    email: $('input#email').val(),
                    password: $('input#password').val(),
                    action: 'login'
                },
                success:function(data)
                {
                    if(data.error === true)
                    {
                        $('#loginLoading').hide(400, function(){
                            $('#loginFeedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn('slow', function(){
                                setTimeout('$(\'#loginFeedback\').fadeOut(\'slow\');', 5000);
                            });
                        });
                    }
                    else
                    {
                        document.location="/sandbox1/myAccount.php";
                    }
                },
		error:function(XMLHttpRequest,textStatus,errorThrown)
		{
                    $('#loginLoading').fadeOut('slow', function(){
                        $('#loginFeedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (my account)').fadeIn('slow', function(){
                            setTimeout('$(\'#loginFeedback\').fadeOut(\'slow\');', 5000);
                        });
                    });
					
                }
            });
            return false;
        }
    });

    //Register Form Actions
    $('#register').click(function(){
        if($('input#companyName').val() == "" || $('input#contactName').val() == "" || $('input#emailReg').val() == "" || $('input#passwordReg').val() == "" ||  $('input#captcha').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please fill out all the fields above!').fadeIn();
            return false;
        }
        else
        {
            $('#feedback').hide();
            $('#loading').fadeIn();

            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    companyName	: $('input#companyName').val(),
                    contactName	: $('input#contactName').val(),
                    emailReg	: $('input#emailReg').val(),
                    passwordReg	: $('input#passwordReg').val(),
                    captcha	: $('input#captcha').val(),
                    action: 'register'
                },
                success:function(data)
                {
                    $('#loading').hide(400, function(){
                        $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn('slow', function(){
                            setTimeout('$(\'#feedback\').fadeOut(\'slow\');', 5000);
                        });
                    });

                    if(data.error === true)
                    {

                    }
                    else
                    {
                        $('#memberRegisterForm').fadeOut('slow', function(){
                                setTimeout('document.location="/sandbox1/login.php";', 5000);
                        });
                    }
				},
				error:function(XMLHttpRequest,textStatus,errorThrown)
				{      
					$('#memberRegisterForm').fadeOut('slow', function(){
								$('#feedback').removeClass().addClass('success').text('Your Account was Successfully Created.').fadeIn();
                                setTimeout('document.location="/sandbox1/login.php";', 5000);
                    });
                    //$('#loading').fadeOut();
                    //$('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (register)').fadeIn();
                }
            });
            return false;
        }
    });
	
	
	    //Update User Form Actions
    $('#empChanges').click(function(){
        if($('input#companyName').val() == "" || $('input#contactName').val() == "" || $('input#email').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please fill out all the fields above!').fadeIn();
            return false;
        }
        else
        {
            $('#feedback').hide();
            $('#loading').fadeIn();
			
            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                ({ 
					companyId   : $('input#companyId').val(),
                    companyName	: $('input#companyName').val(),
                    contactName	: $('input#contactName').val(),
                    email   	: $('input#email').val(),
                    action: 'UpdateEmp'
                }),
                success:function(data)
			
                { 
                    $('#loading').hide(400, function(){						 
                        $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn('slow', function(){
                            setTimeout('$(\'#feedback\').fadeOut(\'slow\');', 5000);
                        });
                    });

                    if(data.error === true)
                    {
						 $('#memberRegisterForm').fadeOut('slow', function(){
								$('#feedback').removeClass().addClass('success').text('What did you do?').fadeIn();
                                setTimeout('document.location="/sandbox1/myAccount.php";', 5000);
                        });

                    }
                    else
                    {
                        $('#memberRegisterForm').fadeOut('slow', function(){
								$('#feedback').removeClass().addClass('success').text('Your Account was Successfully Updated.').fadeIn();
                                setTimeout('document.location="/sandbox1/myAccount.php";', 5000);
                        });
                    }
				},
				error:function(XMLHttpRequest,textStatus,errorThrown)
				{   
					$('#loading').fadeOut();
                  	$('#feedback').removeClass().addClass('success').text('Your Account was Successfully Updated.').fadeIn();
					setTimeout('document.location="/sandbox1/myAccount.php";', 2000);
                  /*$('#loading').fadeOut();
                  $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (Update)').fadeIn();*/
                }
				
            });
			
            return false;
        }
    });
	

    //Forgot Password Actions
    $('#forgot-password').click(function(){
        if($('input#forgot-email').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please enter an email so I can work with it!').fadeIn();
            return false;
        }
        else
        {
            $('#feedback').hide();
            $('#loading').fadeIn();

            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    forgotEmail: $('input#forgot-email').val(),
                    action: 'forgotPasssword'
                },
                success:function(data)
                {
                    $('#loading').hide('slow', function(){
                        $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
                    });

                    if(data.error === true)
                    {

                    }
                    else
                    {
                        $('#forgotPasswordForm').fadeOut('slow');
                        setTimeout('document.location="/sandbox1/index.php"', 2500);
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown)
                {
                    $('#loading').fadeOut('slow', function(){
                        $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again!(forgot password)').fadeIn();
                    });
                }
            });
            return false;
        }
    });

    //Forgot Password - Renew Password Actions
    $('#renew-password').click(function(){
        if($('input#re-password').val() == "" || $('input#re-password-2').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please enter a new password for your account!').fadeIn();
            return false;
        }
        else
        {
            $('#feedback').hide();
            $('#loading').fadeIn();

            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    newPass: $('input#re-password').val(),
                    newPass2: $('input#re-password-2').val(),
                    email: emailVariable,
                    action: 'renewPassword'
                },
                success:function(data)
                {
                    $('#loading').hide('slow', function(){
                        $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
                    });

                    if(data.error === true)
                    {

                    }
                    else
                    {
                        $('#newPasswordForm').fadeOut('slow');
                        setTimeout('document.location="/sandbox1/index.php"', 2500);
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown)
                {
                    $('#loading').fadeOut('slow', function(){
                        $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (renew password)').fadeIn();
                    });
                }
            });
            return false;
        }
    });
	

    //Preview Post Actions
    $('#previewPost').click(function(){
        if($('input#jobTitle').val() == "" || $('input#jobCat').val() == "" || $('#description').val() == "" || $('#country').val() == "" || $('#state1').val() == "" || $('#city1').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please fill in all required fields!').fadeIn('slow', function(){
                setTimeout("$('#feedback').fadeOut('slow')", 5000);
            });
            return false;
        }
        else
        {
            $('#feedback').hide();
            $('#loading').fadeIn();
            $('#jobPostForm').submit();
            return false;
        }
    });

    //Submit Post Actions
    $('#submitPost').click(function(){
        $('#feedback').hide();
        $('#loading').fadeIn();

        var state2 = '';
        var state3 = '';
        var state4 = '';
        var state5 = '';
        var city2 = '';
        var city3 = '';
        var city4 = '';
        var city5 = '';
        var hideName = '';
        var hideSalary = '';

        if($('#state2').length > 0) { state2 = $('#state2').val(); }
        if($('#state3').length > 0) { state3 = $('#state3').val(); }
        if($('#state4').length > 0) { state4 = $('#state4').val(); }
        if($('#state5').length > 0) { state5 = $('#state5').val(); }

        if($('#city2').length > 0)  { city2 = $('#city2').val(); }
        if($('#city3').length > 0)  { city3 = $('#city3').val(); }
        if($('#city4').length > 0)  { city4 = $('#city4').val(); }
        if($('#city5').length > 0)  { city5 = $('#city5').val(); }

        if($('#hideName').val() != 'on'){ hideName = 1; } else { hideName = 0; }
        if($('#hideSalary').val() != 'on'){ hideSalary = 1; } else { hideSalary = 0; }

        $.ajax
        ({
            type: 'POST',
            url: 'process.php',
            dataType: 'json',
            data:
            {
                employerID: $('#employerID').val(),
                jobTitle: $('#jobTitle').val(),
                jobDesc: $('#description').val(),
                jobCat: $('#jobCat').val(),
                salary: $('#salary').val(),
                hideName: hideName,
                hideSalary: hideSalary,
                country: $('#country').val(),
                state1: $('#state1').val(),
                state2: state2,
                state3: state3,
                state4: state4,
                state5: state5,
                city1: $('#city1').val(),
                city2: city2,
                city3: city3,
                city4: city4,
                city5: city5,
                action: 'submitPost'
            },
            success:function(data)
            {
                if(data.error === true)
                {
                    $('#loading').hide('slow', function(){
                        $('#feedback').removeClass().addClass('error').text(data.message).fadeIn('slow', function(){
                            setTimeout("$('#feedback').fadeOut('slow');", 5000)
                        });
                    });
                    return false;
                }
                else
                {
                    $('#loading').hide('slow', function(){
                        $('#feedback').removeClass().addClass('success').text(data.message).fadeIn('slow', function(){
                            setTimeout('document.location="/sandbox1/myPostings.php"', 2500);
                        });
                    });
                    return false;
                }
            },
            error:function(XMLHttpRequest,textStatus,errorThrown)
            {
                $('#loading').fadeOut('slow', function(){
                    $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (submit Post)').fadeIn();
                });
            }
        });
        return false;
    });

    //Country Change Actions
    $('#country').change(function(){
        $('#loading').fadeIn();
        var countryID = $("select#country option:selected").val();
        var numLocations = $('.location').length;
            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    countryID: countryID,
                    action:'getStates'
                },
                success:function(data)
                {
                    if(data.error === true)
                    {
                        $('#loading').hide('slow', function(){
                            $('#feedback').removeClass().addClass('error').text(data.message).fadeIn('slow', function(){
                                setTimeout("$('#feedback').fadeOut('slow');", 5000)
                            });
                        });
                    }
                    else
                    {
                        $('#loading').fadeOut();
                        $('select#state1').replaceWith('<select id="state1" name="state1" class="state">'+data.message+'</select>')
                        if($('#state2').length != 0)
                            {
                                $('select#state2').replaceWith('<select id="state2" name="state2" class="state">'+data.message+'</select>')
                            }
                        if($('#state3').length != 0)
                            {
                                $('select#state3').replaceWith('<select id="state3" name="state3" class="state">'+data.message+'</select>')
                            }
                        if($('#state4').length != 0)
                            {
                                $('select#state4').replaceWith('<select id="state4" name="state4" class="state">'+data.message+'</select>')
                            }
                        if($('#state5').length != 0)
                            {
                                $('select#state5').replaceWith('<select id="state5" name="state5" class="state">'+data.message+'</select>')
                            }
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown)
                {
                    $('#loading').fadeOut('slow', function(){
                        $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (change country)').fadeIn();
                    });
                }
            });
            return false;
    })

    //Add Location Function on Post Job Page
    $('#addLocation').click(function(){
        $('#loading').fadeIn();
        var locations = $('.location');
        var num = locations.size();
        num++;
        if(num <= 5)
        {
            if($('#country').val() == '')
            {
                $('#loading').fadeOut();
                var location = '<div class="location"><label for="location">Location: </label><div class="locationInner" style="text-align:left; margin-left:135px; width:400px;"><label for="city'+num+'" style="width:40px;">City: </label><input type="text" name="city'+num+'" id="city'+num+'" class="input city"><br/><label for="state'+num+'" style="width:40px;">State: </label><select name="state'+num+'" id="state'+num+'" class="state" ><option>  </option></select></div>';
                $('#addLoc').before(location);
            }
            else
            {
                var countryID = $("select#country option:selected").val();
                    $.ajax
                    ({
                        type: 'POST',
                        url: 'process.php',
                        dataType: 'json',
                        data:
                        {
                            countryID: countryID,
                            action:'getStates'
                        },
                        success:function(data)
                        {
                            if(data.error === true)
                            {
                                console.log('ERROR GETTING STATES FOR ADDED LOCATION');
                            }
                            else
                            {
                                $('#loading').fadeOut();
                                var location = '<div class="location"><label for="location">Location: </label><div class="locationInner" style="text-align:left; margin-left:135px; width:400px;"><label for="city'+num+'" style="width:40px;">City: </label><input type="text" name="city'+num+'" id="city'+num+'" class="input city"><br/><label for="state'+num+'" style="width:40px;">State: </label><select name="state'+num+'" id="state'+num+'" class="state" >'+data.message+'</select></div>';
                                $('#addLoc').before(location);
                            }
                        },
                        error:function(XMLHttpRequest,textStatus,errorThrown)
                        {
                            $('#loading').fadeOut('slow', function(){
                                $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (add location)').fadeIn();
                            });
                        }
                    });
                    return false;
            }
        }
        else
        {
            $('#feedback').removeClass().addClass('error').text('I\'m sorry, but there is a maximum of five (5) locations per job post').fadeIn('slow', function(){
                setTimeout("$('#feedback').fadeOut('slow')", 5000);
            });
        }
    });

    //Home Search Form Actions
    if($('#searchHome').length != 0)
    {
        //Resets for so if back button is used, the country field is not pre-selected
        $('form#searchHome')[0].reset();
    }

    //Search Actions
    $('#searchButton').click(function(){
        $('#loading').fadeIn();
        if($('#country').val() == '')
        {
            $('#loading').fadeOut('slow', function(){
                $('#feedback').removeClass().addClass('error').text('Please fill in the country field to search.').fadeIn('slow', function(){
                    setTimeout("$('#feedback').fadeOut('slow')", 5000);
                });
            });
        }
        else
        {
            if($('#state1').val() == '')
            {
                $('#loading').fadeOut('slow', function(){
                    $('#feedback').removeClass().addClass('error').text('Please fill in the state field to search.').fadeIn('slow', function(){
                        setTimeout("$('#feedback').fadeOut('slow')", 5000);
                    });
                });
            }
            else
            {
                $('#feedback').hide();
                $('#loading').hide();
                $('#searchHome').submit();
            }
        }
    });

        //Change Password - My Account Actions
    $('#changePassSubmit').click(function(){
	
        if($('input#currentPass').val() == "" || $('input#newPass').val() == "" || $('input#newPass2').val() == "")
        {
            $('#feedback').removeClass().addClass('error').text('Please fill in all password fields!').fadeIn();
            return false;
        }
        else
        {
			if($('input#newPass').val().length < 5)
			{
				$('#feedback').removeClass().addClass('error').text('New password length is too short.').fadeIn();
            	return false;
			}
            $('#feedback').hide();
            $('#loading').fadeIn();
	
            $.ajax
            ({
                type: 'POST',
                url: 'process.php',
                dataType: 'json',
                data:
                {
                    currentPass : $('input#currentPass').val(),
                    newPass     : $('input#newPass').val(),
                    newPass2    : $('input#newPass2').val(),
                    email       : $('input#email').val(),
                    action      : 'changePassword'
                },
                success:function(data)
                {
                    if(data.error === true)
                    {
                        $('#loading').hide('slow', function(){
                            $('#feedback').removeClass().addClass((data.error === true) ? 'error':'success').text(data.message).fadeIn();
                        });
                    }
                    else
                    {
                        $('form#changePass').fadeOut('slow');
                        $('#feedback').removeClass().addClass('success').text('Your password has been successfully changed.').fadeIn();
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown)
                {
					$('#loading').fadeOut();
                  	$('#feedback').removeClass().addClass('success').text('Your password has been successfully changed.').fadeIn();
					setTimeout('document.location="/sandbox1/myAccount.php";', 3000);
                    /*$('#loading').fadeOut('slow', function(){
                        $('#feedback').removeClass().addClass('error').text('I\'m sorry but the server has died, please try again! (change Password)').fadeIn();
                    });*/
                }
            });
            return false;
        }
    });

    $('#rePost').click(function(){
        $id = $(this).attr('id');
    })
});